using System.Collections.Generic;
using UnityEngine;

public class Fri_AIController : MonoBehaviour
{
	public bool isdie;

	public AI_TYPE fri_type;

	public Transform bodyself;

	public bool ismissileaim;

	public Transform m_transform;

	public float AIspeed;

	public Transform targetTra;

	public AIController targetAI;

	public float targettime;

	private Vector3 targetDir;

	private float angle;

	private float distence;

	private float a;

	private float b;

	private float c;

	private float d;

	private float r;

	public Attribute Cur_att;

	public HealthController health_con;

	public float FightDistance = 2000f;

	public Camera maincam;

	public int TypeIndex;

	public int minimapindex;

	private Vector3 curscreenpos;

	public List<missilemove> curaimmissilelist = new List<missilemove>();

	public bool isinit;

	public Effectobj lowhpsmoke;

	public Effectobj explosionobj;

	public Transform effectpoint;

	public float diedtime;

	public FightInfo fightinfo;

	private Vector3 UIscreenpos;

	private float dirflagoffset;

	private float scale_rate;

	private Vector3 tempaimpos;

	private bool tempisaim;

	public void change_fightinfo(AI_TYPE diedtype)
	{
		switch (diedtype)
		{
		case AI_TYPE.aiplane:
			fightinfo.killnum_plane++;
			break;
		case AI_TYPE.aiship:
			fightinfo.killnum_ship++;
			break;
		case AI_TYPE.building:
			fightinfo.killnum_build++;
			break;
		case AI_TYPE.aibunker:
			fightinfo.killnum_build++;
			break;
		case AI_TYPE.aihgtw:
			fightinfo.killnum_build++;
			break;
		case AI_TYPE.aiboss:
			fightinfo.killnum_build++;
			break;
		case AI_TYPE.smallbuild:
			fightinfo.killnum_build++;
			break;
		}
	}

	public virtual void Reset_Fri_AI()
	{
	}

	public void findTarget()
	{
		if (fri_type == AI_TYPE.aiplane)
		{
			targetAI = AIPool.instance.calaitarget(m_transform.position, isplane: true);
		}
		else
		{
			targetAI = AIPool.instance.calaitarget(m_transform.position, isplane: false);
		}
		if (GlobalInf.gameover)
		{
			targetTra = null;
		}
		else if (targetAI != null)
		{
			targetTra = targetAI.m_transform;
		}
		else
		{
			targetTra = null;
		}
	}

	public void Setdied()
	{
		GetDamage(health_con.MaxHP, health_con.ArmorVal, -2);
	}

	public void GetDamage(int hurt, int pene, int parent)
	{
		if (isdie)
		{
			return;
		}
		if (health_con.getDamage(hurt, pene))
		{
			isinit = false;
			isdie = true;
			fightinfo.diednum++;
			AIPool.instance.check_fightinfo(fri_type, parent, TypeIndex);
			if (explosionobj == null)
			{
				switch (fri_type)
				{
				case AI_TYPE.aiplane:
					explosionobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.diedexplosion);
					break;
				case AI_TYPE.aiship:
					explosionobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.diedexplosion_ship);
					break;
				case AI_TYPE.aiboss:
					explosionobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.diedexplosion_ship);
					break;
				case AI_TYPE.building:
					explosionobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.diedexplosion_ship);
					break;
				case AI_TYPE.smallbuild:
					explosionobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.diedexplosion);
					break;
				}
				if (explosionobj != null)
				{
					explosionobj.transform.parent = effectpoint;
					explosionobj.transform.localPosition = Vector3.zero;
					explosionobj.transform.localRotation = Quaternion.Euler(Vector3.zero);
					if (!UnityVersionUtil.IsActive(explosionobj.gameObject))
					{
						UnityVersionUtil.SetActiveRecursive(explosionobj.gameObject, state: true);
					}
				}
			}
			switch (fri_type)
			{
			case AI_TYPE.aiplane:
				MonoInstance<AudioController>.instance.play(AudioType.planeExplosion, Vector3.Distance(m_transform.position, PlaneMoveController.instance.m_transform.position));
				break;
			case AI_TYPE.aiship:
				MonoInstance<AudioController>.instance.play(AudioType.ShipExplosion, Vector3.Distance(m_transform.position, PlaneMoveController.instance.m_transform.position));
				break;
			case AI_TYPE.aiboss:
				MonoInstance<AudioController>.instance.play(AudioType.BuildExplosion, Vector3.Distance(m_transform.position, PlaneMoveController.instance.m_transform.position));
				break;
			case AI_TYPE.building:
				MonoInstance<AudioController>.instance.play(AudioType.BuildExplosion, Vector3.Distance(m_transform.position, PlaneMoveController.instance.m_transform.position));
				break;
			case AI_TYPE.smallbuild:
				MonoInstance<AudioController>.instance.play(AudioType.BuildExplosion, Vector3.Distance(m_transform.position, PlaneMoveController.instance.m_transform.position));
				break;
			}
		}
		if (health_con.check_lowhp() && lowhpsmoke == null)
		{
			switch (fri_type)
			{
			case AI_TYPE.aiplane:
				lowhpsmoke = EffectPool.instance.GetEffectObj(EFFECTTYPE.lowhpsmoke);
				break;
			case AI_TYPE.aiship:
				lowhpsmoke = EffectPool.instance.GetEffectObj(EFFECTTYPE.lowhpsmoke_ship);
				break;
			case AI_TYPE.aihgtw:
				lowhpsmoke = EffectPool.instance.GetEffectObj(EFFECTTYPE.lowhpsmoke_building);
				break;
			case AI_TYPE.building:
				lowhpsmoke = EffectPool.instance.GetEffectObj(EFFECTTYPE.lowhpsmoke_building);
				break;
			}
			if (lowhpsmoke != null)
			{
				lowhpsmoke.transform.parent = effectpoint;
				lowhpsmoke.transform.localPosition = Vector3.zero;
				lowhpsmoke.transform.localRotation = Quaternion.Euler(Vector3.zero);
				if (!UnityVersionUtil.IsActive(lowhpsmoke.gameObject))
				{
					UnityVersionUtil.SetActiveRecursive(lowhpsmoke.gameObject, state: true);
				}
			}
		}
		changeHP();
	}

	public void diedfun()
	{
		diedtime += Time.deltaTime;
		if (!(diedtime > 5f))
		{
			return;
		}
		diedtime = 0f;
		if (lowhpsmoke != null)
		{
			switch (fri_type)
			{
			case AI_TYPE.aiplane:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.lowhpsmoke, lowhpsmoke);
				break;
			case AI_TYPE.aiship:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.lowhpsmoke_ship, lowhpsmoke);
				break;
			case AI_TYPE.aihgtw:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.lowhpsmoke_building, lowhpsmoke);
				break;
			case AI_TYPE.building:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.lowhpsmoke_building, lowhpsmoke);
				break;
			}
		}
		if (explosionobj != null)
		{
			switch (fri_type)
			{
			case AI_TYPE.aiplane:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.diedexplosion, explosionobj);
				break;
			case AI_TYPE.aiship:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.diedexplosion_ship, explosionobj);
				break;
			case AI_TYPE.aiboss:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.diedexplosion_ship, explosionobj);
				break;
			case AI_TYPE.building:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.diedexplosion_ship, explosionobj);
				break;
			case AI_TYPE.smallbuild:
				EffectPool.instance.RecycleEffect(EFFECTTYPE.diedexplosion, explosionobj);
				break;
			}
		}
		lowhpsmoke = null;
		explosionobj = null;
		UnityVersionUtil.SetActiveRecursive(base.gameObject, state: false);
	}

	public void changeHP()
	{
		UIController.instance.fri_changeHP(TypeIndex, health_con.CurrentHP, health_con.MaxHP);
	}

	public void addmissile(missilemove mis)
	{
		curaimmissilelist.Add(mis);
	}

	public void decaimMissile(missilemove mis)
	{
		curaimmissilelist.RemoveAt(curaimmissilelist.IndexOf(mis));
		mis.ishaveaim = false;
	}

	public void showHPanddir()
	{
		dirflagoffset = 32f * GlobalInf.ratioReltoUI;
		curscreenpos = maincam.WorldToScreenPoint(bodyself.position);
		if (curscreenpos.x > 0f && curscreenpos.y > 0f && curscreenpos.x < (float)Screen.width && curscreenpos.y < (float)Screen.height && curscreenpos.z > 0f)
		{
			if (GlobalInf.gamemode == TASKMODE.chanllenge && fri_type == AI_TYPE.aiplane)
			{
				UIController.instance.fri_nameshow(curscreenpos, TypeIndex, istrue: true);
			}
			UIController.instance.fri_showHP(curscreenpos, TypeIndex, istrue: true);
			UIController.instance.fri_dirshow(Vector3.zero, TypeIndex, istrue: false);
			return;
		}
		scale_rate = (FightDistance - Vector3.Distance(bodyself.position, maincam.transform.position)) / FightDistance + 0.4f;
		if (scale_rate < 0.4f)
		{
			scale_rate = 0.4f;
		}
		if (scale_rate > 0.6f)
		{
			scale_rate = 0.6f;
		}
		dirflagoffset *= scale_rate;
		if (GlobalInf.gamemode == TASKMODE.chanllenge && fri_type == AI_TYPE.aiplane)
		{
			UIController.instance.fri_nameshow(Vector3.zero, TypeIndex, istrue: false);
		}
		UIController.instance.fri_showHP(Vector3.zero, TypeIndex, istrue: false);
		if (curscreenpos.z > 0f)
		{
			UIscreenpos = curscreenpos;
			if (UIscreenpos.x < 0f)
			{
				UIscreenpos.x = dirflagoffset;
			}
			if (UIscreenpos.x > (float)Screen.width)
			{
				UIscreenpos.x = (float)Screen.width - dirflagoffset;
			}
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
			UIController.instance.fri_dirshow(UIscreenpos, TypeIndex, istrue: true, scale_rate, fri_type);
			return;
		}
		UIscreenpos = curscreenpos;
		UIscreenpos.x = (float)Screen.width - UIscreenpos.x;
		UIscreenpos.y = (float)Screen.height - UIscreenpos.y;
		Vector3 vector = maincam.transform.InverseTransformPoint(bodyself.position);
		if (vector.x > 0f)
		{
			UIscreenpos.x = (float)Screen.width - dirflagoffset;
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
		}
		else
		{
			UIscreenpos.x = dirflagoffset;
			if (UIscreenpos.y < 0f)
			{
				UIscreenpos.y = dirflagoffset;
			}
			if (UIscreenpos.y > (float)Screen.height)
			{
				UIscreenpos.y = (float)Screen.height - dirflagoffset;
			}
		}
		UIController.instance.fri_dirshow(UIscreenpos, TypeIndex, istrue: true, scale_rate, fri_type);
	}

	public void disappearhp_dir()
	{
		if (GlobalInf.gamemode == TASKMODE.chanllenge && fri_type == AI_TYPE.aiplane)
		{
			UIController.instance.fri_nameshow(Vector3.zero, TypeIndex, istrue: false);
		}
		UIController.instance.fri_showHP(Vector3.zero, TypeIndex, istrue: false);
		UIController.instance.fri_dirshow(Vector3.zero, TypeIndex, istrue: false);
	}

	public void minimapshow()
	{
		minimapcontroller instance = minimapcontroller.instance;
		AI_TYPE type = fri_type;
		Vector3 position = m_transform.position;
		Vector3 eulerAngles = m_transform.eulerAngles;
		instance.fri_show_flag(type, position, eulerAngles.y, minimapindex);
	}

	public void minimapdis()
	{
		minimapcontroller instance = minimapcontroller.instance;
		AI_TYPE type = fri_type;
		Vector3 position = m_transform.position;
		Vector3 eulerAngles = m_transform.eulerAngles;
		instance.fri_show_flag(type, position, eulerAngles.y, minimapindex, isdie: true);
	}

	public Vector3 CalcAimPos(Vector3 bulletpos, float gunspeed)
	{
		tempaimpos = Vector3.zero;
		if (AIspeed < 1f)
		{
			tempisaim = true;
			tempaimpos = bodyself.position;
		}
		else
		{
			targetDir = bulletpos - bodyself.position;
			angle = Vector3.Angle(targetDir, bodyself.forward);
			distence = targetDir.magnitude;
			a = MyMath.GetPom(gunspeed / AIspeed, 2f);
			b = MyMath.GetRad(distence, angle);
			c = distence * distence;
			d = MyMath.GetDelta(a, b, c);
			tempisaim = (d >= 0f && !float.IsNaN(d) && !float.IsInfinity(d));
			if (tempisaim)
			{
				r = MyMath.GetSqrtOfMath(a, b, d);
				if (r < 0f)
				{
					tempisaim = false;
				}
				tempaimpos = bodyself.position + bodyself.forward * r;
			}
		}
		if (tempaimpos == Vector3.zero || !tempisaim)
		{
			tempaimpos = bodyself.position;
		}
		return tempaimpos;
	}
}
